/*
 * Copyright @ 2014 com.iflysse.trains
 * manger_v2.0 下午4:39:37
 * All right reserved.
 *
 */
package com.iflysse.servlet.site;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iflysse.pojo.SiteInfo;
import com.iflysse.service.SiteInfoService;
import com.iflysse.servlet.util.BaseServlet;

/**
 * @desc: manger_v2.0
 * @author: Administrator
 * @createTime: 2014年11月20日 下午4:39:37
 * @history:
 * @version: v1.0
 */

@WebServlet("/site/site.do")
public class SiteServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 *
	 * @author: Administrator
	 * @createTime: 2014年11月22日 下午8:44:12
	 * @history:
	 * @param req
	 * @param resp
	 *            void
	 */
	private void submit(HttpServletRequest req, HttpServletResponse resp) {

		String name = req.getParameter("siteName");
		String institutions = req.getParameter("institutions");
		String keyword = req.getParameter("keyword");
		String seodesc = req.getParameter("seodesc");
		String seotitle = req.getParameter("seotitle");

		if (name.equals("")) {
			name = null;
		}
		if (institutions.equals("")) {
			institutions = null;
		}
		if (seodesc.equals("")) {
			seodesc = null;
		}
		if (seotitle.equals("")) {
			seotitle = null;
		}
		if (keyword.equals("")) {
			keyword = null;
		}
		SiteInfo site = new SiteInfo();

		site.setName(name);
		site.setInstitutions(institutions);
		site.setKeyword(keyword);
		site.setSeodesc(seodesc);
		site.setSeotitle(seotitle);
//		System.out.println(site);

		SiteInfoService service1 = new SiteInfoService();
		if (service1.findSite() == null) {
			service1.insertSite(site);
		} else {
			service1.updateSite(site);
		}
		try {
			resp.sendRedirect(req.getContextPath()+"/site/site.do");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *
	 * @author: Administrator
	 * @createTime: 2014年11月21日 上午10:15:30
	 * @history:
	 * @param req
	 * @param resp
	 *            void
	 */
	private void query(HttpServletRequest req, HttpServletResponse resp) {

		SiteInfo site = new SiteInfo();
		SiteInfoService service = new SiteInfoService();
		site = service.findSite();
		req.setAttribute("site", site);
	}

	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("edit".equals(method)) {
			request.getRequestDispatcher("/WEB-INF/views/site/siteEdit.jsp")
					.forward(request, response);
		} else if ("tijiao".equals(method)) {
			submit(request, response);
		} else {
			query(request, response);
			forward(request, response, "/WEB-INF/views/site/site.jsp");
		}

	}

}
